﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using SchemaExplorer;
using CodeSmith.CustomProperties;
using CodeSmith.Engine;
using System.IO;
using System.Linq;

namespace Maydear
{
    #region 公共后台模板基类

    /// <summary>
    /// 公共后台模板基类
    /// </summary>
    public class Helper : CodeTemplate
    {
        #region 公有属性


        #endregion

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public Helper()
        {

        }

        #endregion

        #region 公共方法
        
        /// <summary>
        /// 格式化表及列名称
        /// </summary>
        /// <param name="name">待格式化名称</param>
        /// <returns>返回格式化后的名称</returns>
        public string FormatName(string name)
        {
            string text = string.Empty;

            var array = name.Split(new char[]
	        {
		        '_'
	        });
            for (int i = 0; i < array.Length; i++)
            {
                text = text + array[i].Substring(0, 1).ToUpper() + array[i].Substring(1).ToLower();
            }
            return text;
        }
        
        /// <summary>
        /// 截断前缀
        /// </summary>
        /// <param name="name">待格式化名称</param>
        /// <returns>返回格式化后的名称</returns>
        public string CutOffPrefix(string name)
        {
            return name.Substring(name.IndexOf('_'));
        }

        /// <summary>
        /// 根椐文件路径获取文件OutputFile对象
        /// </summary>
        /// <param name="fileName">文件绝对路径</param>
        /// <param name="dependentUpon"></param>
        /// <param name="metaData"></param>
        /// <returns>返回文件OutputFile对象</returns>
        public OutputFile GetOutputFile(string fileName, string dependentUpon, params object[] metaData)
        {
            OutputFile outputFile = new OutputFile(fileName);

            if (!String.IsNullOrEmpty(dependentUpon))
                outputFile.DependentUpon = Path.GetFullPath(dependentUpon);

            if (metaData.Length % 2 != 0)
                throw new Exception("Invalid Metadata: Provide 2 objects per entry, a String (key) followed by an Object.");

            for (int x = 0; x < metaData.Length; x += 2)
                outputFile.Metadata.Add(metaData[x].ToString(), metaData[x + 1]);

            return outputFile;
        }

        /// <summary>
        /// 获取指定文件夹绝对路径
        /// </summary>
        /// <param name="folder">文件夹名称</param>
        /// <param name="folder">基目录</param>
        /// <returns>返回文件夹绝对路径</returns>
        public string GetFolder(string folder,string baseDirectory)
        {
            string current = string.IsNullOrWhiteSpace(baseDirectory) ? Directory.GetCurrentDirectory() : baseDirectory;
            string path = Path.GetFullPath(current);

            return CreateFolder(path, folder);
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path">当前路径</param>
        /// <param name="folder">要创建的文件夹名称</param>
        /// <returns>返回新创建文件夹绝对路径</returns>
        private string CreateFolder(string path, string folder)
        {
            if (String.IsNullOrEmpty(folder))
                return path;

            string newPath = Path.Combine(path, folder);
            if (!Directory.Exists(newPath))
                Directory.CreateDirectory(newPath);

            return newPath;
        }

        /// <summary>
        /// 获取文件绝对路径
        /// </summary>
        /// <param name="localPath">文件相对路径</param>
        /// <returns>返回文件绝对路径</returns>
        public string GetFile(string localPath)
        {
            var path = Path.Combine(this.CodeTemplateInfo.DirectoryName, localPath);
            return Path.GetFullPath(path);
        }
        
        /// <summary>
        /// 判断列是存在于表中
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="columnName">列名</param>
        /// <returns>存在则返回true，否则返回false</returns>
        public bool IsColumnExistsInTable(TableSchema table,string columnName)
        {
            return table.Columns.Any(e=>FormatName(e.Name) == columnName) || table.Columns.Any(e=>e.Name == columnName);
        }

        #endregion
    }

    #endregion

    #region 公共前台模板基类

    /// <summary>
    /// 公共方法
    /// </summary>
    public class ViewHelper : Helper
    {
        #region 公有属性

        /// <summary>
        /// 导航菜单CSS名称
        /// </summary>
        [Optional, Category("2.Title"), Description("导航菜单CSS样式名称组")]
        public NameValueCollection TitleCssClass { get; set; }

        #endregion

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public ViewHelper()
        {
            if (TitleCssClass == null)
            {
                TitleCssClass = new NameValueCollection();
            }

            TitleCssClass.Add("系统管理", "fa fa-cogs");
            TitleCssClass.Add("管理员管理", "fa fa-anchor");
            TitleCssClass.Add("角色管理", "fa fa-group");
            TitleCssClass.Add("会员等级管理", "fa fa-trophy");
            TitleCssClass.Add("客户等级管理", "fa fa-leaf");
            TitleCssClass.Add("投顾等级管理", "fa fa-leaf");
            TitleCssClass.Add("审批流程管理", "fa fa-puzzle-piece");
            TitleCssClass.Add("用户管理", "fa fa-user");
            TitleCssClass.Add("客户管理", "fa fa-leaf");
            TitleCssClass.Add("投顾管理", "fa fa-leaf");
            TitleCssClass.Add("投顾审核", "fa fa-leaf");
        }

        #endregion

        #region 公共方法

        /// <summary>
        /// 格式化表及列名称(BASE_USER->BaseUser)
        /// </summary>
        /// <param name="name">待格式化名称</param>
        /// <returns>返回格式化后的名称</returns>
        public string FormatName(string name)
        {
            var array = name.Split(new char[]
	        {
		        '_'
	        });

            string text = string.Empty;
            for (int i = 0; i < array.Length; i++)
            {
                text = text + array[i].Substring(0, 1).ToUpper() + array[i].Substring(1).ToLower();
            }
            return text;
        }
        
        /// <summary>
        /// 截断前缀
        /// </summary>
        /// <param name="name">待格式化名称</param>
        /// <returns>返回格式化后的名称</returns>
        public string CutOffPrefix(string name)
        {
            return name.Substring(name.IndexOf('_'));
        }

        /// <summary>
        /// 根椐文件路径获取文件OutputFile对象
        /// </summary>
        /// <param name="fileName">文件绝对路径</param>
        /// <param name="dependentUpon"></param>
        /// <param name="metaData"></param>
        /// <returns>返回文件OutputFile对象</returns>
        public OutputFile GetOutputFile(string fileName, string dependentUpon, params object[] metaData)
        {
            OutputFile outputFile = new OutputFile(fileName);

            if (!String.IsNullOrEmpty(dependentUpon))
                outputFile.DependentUpon = Path.GetFullPath(dependentUpon);

            if (metaData.Length % 2 != 0)
                throw new Exception("Invalid Metadata: Provide 2 objects per entry, a String (key) followed by an Object.");

            for (int x = 0; x < metaData.Length; x += 2)
                outputFile.Metadata.Add(metaData[x].ToString(), metaData[x + 1]);

            return outputFile;
        }

        /// <summary>
        /// 获取指定文件夹绝对路径
        /// </summary>
        /// <param name="folder">文件夹名称</param>
        /// <param name="folder">基目录</param>
        /// <returns>返回文件夹绝对路径</returns>
        public string GetFolder(string folder, string baseDirectory)
        {
            string current = string.IsNullOrWhiteSpace(baseDirectory) ? Directory.GetCurrentDirectory() : baseDirectory;
            string path = Path.GetFullPath(current);

            return CreateFolder(path, folder);
        }

        /// <summary>
        /// 创建文件夹
        /// </summary>
        /// <param name="path">当前路径</param>
        /// <param name="folder">要创建的文件夹名称</param>
        /// <returns>返回新创建文件夹绝对路径</returns>
        private string CreateFolder(string path, string folder)
        {
            if (String.IsNullOrEmpty(folder))
                return path;

            string newPath = Path.Combine(path, folder);
            if (!Directory.Exists(newPath))
                Directory.CreateDirectory(newPath);

            return newPath;
        }

        /// <summary>
        /// 获取文件绝对路径
        /// </summary>
        /// <param name="localPath">文件相对路径</param>
        /// <returns>返回文件绝对路径</returns>
        public string GetFile(string localPath)
        {
            var path = Path.Combine(this.CodeTemplateInfo.DirectoryName, localPath);
            return Path.GetFullPath(path);
        }

        #endregion
    }

    #endregion

    #region 字段分组

    /// <summary>
    /// 字段分组类
    /// </summary>
    public class FieldsGroup
    {
        /// <summary>
        /// 组名称
        /// </summary>
        public string GroupName { get; set; }

        /// <summary>
        /// 组下属字段信息
        /// </summary>
        public IList<FieldInfo> FieldsInfo { get; set; }
    }

    /// <summary>
    /// 字段属性
    /// </summary>
    public class FieldInfo
    {
        /// <summary>
        /// 字段名
        /// </summary>
        public string FieldName { get; set;}

        /// <summary>
        /// 描述信息
        /// </summary>
        public string FieldDescription { get; set;}

        /// <summary>
        /// 排序
        /// </summary>
        public int Sort { get; set; }
    }

    #endregion
}
